#include <iostream>
#include <mysql/mysql.h>
// 端口错误或mysqld未启动都会导致连接失败
// ctrl+d：事务流结束 有些平台如windows是ctrl+c

// const std::string host ="localhost";
const std::string host = "127.0.0.1";
const std::string user = "lhr";
const std::string passwd = "lhr.547521";
const std::string db = "c_cpp_test";
const unsigned int port = 3306;

int main()
{
    printf("mysql client Version: %s\n\n", mysql_get_client_info());

    // 句柄：fd/file*/c++文件流对象
    MYSQL *_mysql = mysql_init(nullptr);
    if (nullptr == _mysql)
    {
        fprintf(stderr, "%s\n", mysql_error(_mysql));
        return 1;
    }

    if (mysql_real_connect(_mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        fprintf(stderr, "%s\n", mysql_error(_mysql));
        mysql_close(_mysql);
        return 1;
    }
    // 建立好链接之后 获取英文没有问题 获取中文是乱码
    // 需要设置链接的默认字符集是utf8 客户端原始默认是latin1
    // 而我们设置了mysqld的默认字符集是utf8 编码解码不一致！
    mysql_set_character_set(_mysql, "utf8");

    // std::string sql ="update user set name='jimmy' where id=2";
    // std::string sql = "insert into user (name, age, phone) values('张三',19,'654321987687')";
    // std::string sql="delete from user where id=1";
    std::string sql = "select * from user";
    // std::string sql="select name,age from user where id=1";
    int value = mysql_query(_mysql, sql.c_str());
    if (value == 0)
        std::cout << "Query OK" << std::endl;
    else
    {
        fprintf(stderr, "%s\n", mysql_error(_mysql));
        mysql_close(_mysql);
        return 1;
    }

    MYSQL_RES *res = mysql_store_result(_mysql);
    if (nullptr == res)
    {
        fprintf(stderr, "%s\n", mysql_error(_mysql));
        mysql_close(_mysql);
        return 1;
    }
    my_ulonglong rows = mysql_num_rows(res);
    unsigned int cols = mysql_num_fields(res);
    MYSQL_FIELD *fields = mysql_fetch_fields(res);

    std::cout << std::endl;
    std::cout << "select database(): " << fields[0].db << std::endl;
    std::cout << "select table(): " << fields[0].table << std::endl;
    std::cout << std::endl;

    for (int i = 0; i < cols; i++)
        std::cout << fields[i].name << " ";
    std::cout << std::endl;

    ///* return data as array of strings */
    // typedef char **MYSQL_ROW;
    MYSQL_ROW line;
    for (int i = 0; i < rows; i++)
    {
        line = mysql_fetch_row(res);
        for (int j = 0; j < cols; j++)
            std::cout << line[j] << " ";
        std::cout << std::endl;
    }

    /*
    std::string sql;
    while (true)
    {
        std::cout << "MySOL>>>";
        if (!std::getline(std::cin, sql) || sql == "quit")
        {
            std::cout << "bye bye" << std::endl;
            break;
        }
        int n = mysql_query(_mysql, sql.c_str());
        if (n == 0)
            std::cout << sql << "success:" << n << std::endl;
        else
            mysql_close(_mysql);
    }
    std::cout << "connect susccess" << std::endl;
    */
    mysql_free_result(res);
    mysql_close(_mysql);
    return 0;
}